package com.mkj.progress;

import javax.swing.*;
import java.awt.*;

/**
 * @Author Arrebol
 * @Date 2024/8/6 20:53
 * @Project graphical programming
 * @Description：
 * 进度条组件基本演示
 */
public class JProgressBarTest1 {

    private final JFrame frame = new JFrame("测试进度条");
    JCheckBox uncertainProgress = new JCheckBox("不确定进度");
    JCheckBox noBorder = new JCheckBox("不绘制边框");
    //创建进度条
    JProgressBar progressBar  = new JProgressBar(JProgressBar.HORIZONTAL,0,100);

    public void init(){
        //组装视图
        //处理复选框的点击行为
        uncertainProgress.addActionListener(e -> {
            //获取复选框是否选中
            boolean isSelected = uncertainProgress.isSelected();
            //设置当前进度条不确定进度
            progressBar.setIndeterminate(isSelected);
            progressBar.setStringPainted(!isSelected);
        });
        noBorder.addActionListener(e -> {
            boolean isSelected = noBorder.isSelected();
            progressBar.setBorderPainted(!isSelected);
        });
        Box vBox = Box.createVerticalBox();
        vBox.add(uncertainProgress);
        vBox.add(noBorder);
        //设置进度条的属性
        progressBar.setStringPainted(true);
        progressBar.setBorderPainted(true);
        //将当前窗口布局设置为流式布局
        frame.setLayout(new FlowLayout());
        frame.add(vBox);
        frame.add(progressBar);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        //通过循环模拟进度条进度
        for (int i = 1; i <= 100; i++) {
            //修改已经完成的工作量，也就是百分比
            progressBar.setValue(i);
            try {
                Thread.sleep(250);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        new JProgressBarTest1().init();
    }
}
